package com.benjaminwan.read.free.utils

import androidx.fragment.app.Fragment
import com.airbnb.mvrx.MvRx
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

fun <V : Any> argsNullable() = object : ReadOnlyProperty<Fragment, V?> {
    var value: V? = null

    override fun getValue(thisRef: Fragment, property: KProperty<*>): V? {
        if (value == null) {
            val args = thisRef.arguments ?: return null
            val argUntyped = args.get(MvRx.KEY_ARG)
            argUntyped ?: return null
            @Suppress("UNCHECKED_CAST")
            value = argUntyped as V
        }
        return value
    }
}